<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class IndexController extends JLControllerBase {

    private $path = "Index";
    private $title = "Système d'Actualisation CSME";
    private $data = NULL;

    /**
     * Funcion por defecto para los controladores
     */
    public function Index() {
        if ($this->logueado()) {
            $this->View->Show($this->path, $this->title, array(
                'classcurrent' => ' class="current"'
            ));
        }
    }

    public function Login() {
        if ($this->Ajax->isAjaxRequest()) {
            if ($this->Ajax->isValidToken()) {
                $this->Ajax->resetToken();

                if ($this->logueado()) {
                    $this->Ajax->response(RESPONSE_CODE_OK);
                }

                $user = filter_var($_POST['_login'], FILTER_SANITIZE_STRING);
                $pwd = filter_var($_POST['_pwd'], FILTER_SANITIZE_STRING);

                if (!empty($user) && !empty($pwd)) {
                    require JLPATH_MODELS . DS . 'LoginModel.php';
                    $login = new LoginModel();
                    $data = $login->login($user, md5($pwd));
                    $this->data = $data;

                    if ($data) {
                        $this->CreateSession();
                        $this->Ajax->response(RESPONSE_CODE_OK);
                    } else {
                        $this->Ajax->response(RESPONSE_CODE_ALERT);
                    }
                } else {
                    $this->Ajax->response(RESPONSE_CODE_ERROR);
                }
            } else {
                $this->Ajax->forbiddenHeader();
            }
        } else {
            header("Location: ../");
            exit();
        }
    }

    /**
     * Crea la sesion de usuario
     * @Retorna bool
     */
    public function CreateSession() {
        session_regenerate_id();

        if ($this->data['int_id_typeuser'] == 1) {
            $_SESSION['form_user'] = "jlqs";
            $_SESSION['form_pwd'] = "joseluis10";
        }

        $_SESSION['_session_id'] = session_id();
        $_SESSION['_int_id_user'] = $this->data['int_id_user'];
        $_SESSION['_var_fullname'] = $this->data['var_fullname'];
        $_SESSION['_var_email'] = $this->data['var_email'];
        $_SESSION['_var_username'] = $this->data['var_username'];
        $_SESSION['_dat_creation'] = $this->data['dat_creation'];
        $_SESSION['_int_id_section'] = $this->data['int_id_section'];
        $_SESSION['_int_id_sections_view'] = intval($this->data['int_id_section']) - 2;
        $_SESSION['_int_id_typeuser'] = $this->data['int_id_typeuser'];
        $_SESSION['_var_desc_typeuser'] = $this->data['var_desc_typeuser'];
        $_SESSION['_var_desc_section'] = $this->data['var_desc_section'];
        $_SESSION['_int_id_state'] = $this->data['int_id_state'];
        $_SESSION['_var_htmldesc'] = $this->data['var_htmldesc'];
    }

    /**
     * Crea la sesion de usuario
     * @Retorna bool
     */
    public function logout($redirect = TRUE) {
        if ($this->logueado()) {
            session_regenerate_id();
            $this->Ajax->resetToken();

            unset($_SESSION["form_user"]);
            unset($_SESSION["form_pwd"]);
            unset($_SESSION["_session_id"]);
            unset($_SESSION["_int_id_user"]);
            unset($_SESSION["_var_fullname"]);
            unset($_SESSION["_var_email"]);
            unset($_SESSION["_var_username"]);
            unset($_SESSION["_dat_creation"]);
            unset($_SESSION["_int_id_section"]);
            unset($_SESSION["_int_id_sections_view"]);
            unset($_SESSION["_int_id_typeuser"]);
            unset($_SESSION["_var_desc_typeuser"]);
            unset($_SESSION["_var_desc_section"]);
            unset($_SESSION["_int_id_state"]);
            unset($_SESSION["_var_htmldesc"]);

            if ($redirect) {
                echo('
                  <body bgcolor="#133848">
                       <center>
                        <font face="Georgia, Times New Roman, Times, serif" color=#ffb41e>Un instant s&rsquo;il vous pla&icirc;t...</font>
                        </center>
                  </body>
            ');

                echo "<script>document.location='../'</script>";
            }
        }
    }

    public function required() {
        $csrf = $this->Ajax->getToken();
        $data = "(function($){\$('head').prepend($('<meta/>').attr({'name':'csrf-token','content':'$csrf'}))})(jQuery);";
        $this->headerJavascript($data);
        exit();
    }

    public function addViewedCounter() {
        if ($this->Ajax->isAjaxRequest()) {
            $id = filter_var($_GET['c'], FILTER_SANITIZE_NUMBER_INT);
            if (filter_var($id, FILTER_VALIDATE_INT)) {
                require JLPATH_MODELS . DS . "GalleryModel.php";
                $galleries = new GalleryModel();
                $galleries->updateViewed($id);
                $this->Ajax->response(RESPONSE_CODE_OK);
            } else {
                $this->Ajax->response(RESPONSE_CODE_ERROR);
            }
        } else {
            $this->redirect();
        }
    }

}